package com.runa.monitor.platinterface.netservice.equipment.furuisi;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.monitor.platinterface.netservice.equipment.dtu.furuisi.FuRuiSiMessage;
import com.runa.persistence.util.LogUtil;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.dto.DtuReport;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.furuisi.FuRuiSiReportAck;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 2024/12/4 16:00
 * zhangYB
 */
@Slf4j
public class FuRuiSiClientAnalysis extends ShellBufferAnalysis<byte[]> {

    public FuRuiSiClientAnalysis() {
        super(null, 0);
    }

    @Override
    public int fill(byte[] data, int length, String netEquNo) {
        this.interData = null;
        netEquNo = FuRuiSiClientDriver.FRS_NAME;
        // 抄阀 SIF-24107038-01030801F401F4022E013EB021-
        // 控阀 SIF-24107038-0106002103E8D97E-
        String message = new String(data, 0, length);
        LogUtil.getLogAndInfo(netEquNo, "弗瑞斯", message);
        if (message.contains("Please login")) {
            this.interData = new FuRuiSiMessage("LOGIN");
        } else if ("Success".equalsIgnoreCase(message)) {
            LogUtil.info(netEquNo, "注册成功");
            this.interData = new FuRuiSiMessage("LOGIN_SUCCESS");
        } else if (message.startsWith("SIF")) {
            String[] split = message.split("-");
            if (split.length >= 3) {
                String serial = split[1];
                String body = split[2];
                valveHandle(serial, body);
            }
        }
//        else if ("DTU IS NOT EXIST".equalsIgnoreCase(message)) {
//
//        }
        return 0;
    }

    public void valveHandle(String serial, String body) {
        ValveVO valveVO = new ValveVO();
        valveVO.setReadTime(new Date());
        byte[] data = ByteUtil.hexStringToByte(body);
        if ((data[1] & 0xff) == 0x03) {
            readValveResponse(serial, data);
        } else if ((data[1] & 0xff) == 0x06) {
            this.interData = new RunaConcenAffirmDeny(FuRuiSiClientDriver.FRS_NAME, true);
        }
    }

    public void readValveResponse(String serial, byte[] data) {
        ValveVO valveVO = new ValveVO();
        valveVO.setReadTime(new Date());
        valveVO.setValveSerial(serial);
        valveVO.setIndex(data[0] & 0xff);
        int len = data[2];
        if (len != 8 && data.length < 3 + 8) {
            return;
        }
        int index = 3;
        ByteUtil.byteToUShort(data, index, ByteOrder.AB);
        valveVO.setActOpen(((float)ByteUtil.byteToUShort(data, index, ByteOrder.AB))/10);
        index += 2;
        index += 2;
        valveVO.setSupplyTemp(((float)ByteUtil.byteToShort(data, index, ByteOrder.AB))/10);
        index += 2;
        valveVO.setReturnTemp(((float)ByteUtil.byteToShort(data, index, ByteOrder.AB))/10);

        RunaConcenResponseData response = new RunaConcenResponseData();
        response.setAddress(FuRuiSiClientDriver.FRS_NAME);
        response.addRepData(valveVO);
        this.interData = response;
    }

}
